unit libBasic_CustomD;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, ADODB, libTypesU;

type
  TdmBasic_Custom = class(TDataModule)
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    lstCalculate:TStringList;
    FPriceVisible: Boolean; 
    procedure doCalculate(DataSet:TADODataset; lstPACalculate:TStringList);overload;
    procedure dsfldGetText(Sender: TField; var Text: String;DisplayText: Boolean);
    procedure dsfldSetText(Sender: TField; const Text: String);
    procedure doSetFieldVisible(FieldVisible: boolean);
    procedure SetPriceVisible(const Value: Boolean);
  protected
    procedure doAddCalcList(DataSet:TDataSet;SearchListType:TSearchList;sKeyFieldName,
      sCalcFieldName:string;bMultiLanguage:boolean=False);
    function GetDescription(SearchListType:TSearchList;sPAKeyValue:string;bMultiLanguage:boolean=False):variant;
    procedure doCalculate(DataSet:TDataset); overload;
    property PriceVisible: Boolean read FPriceVisible write SetPriceVisible;
  end;

implementation

uses
  TypInfo, libParamsU, MainModule;

{$R *.DFM}

const
  cSpliter = '|';
{ TdmBasic }
{------------------------------------------------------------------------------}
function SearchListToStr(SL: TSearchList): string;
begin
  Result := GetEnumName(TypeInfo(TSearchList), Integer(SL));
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.doAddCalcList(DataSet:TDataSet; SearchListType:TSearchList;
  sKeyFieldName, sCalcFieldName:string;bMultiLanguage:boolean);
var
  sAdd: string;
begin
  sAdd := '%s' + cSpliter + '%s' + cSpliter + '%s' + cSpliter + '%s' + cSpliter + BoolToStr(bMultiLanguage);
  sAdd := Format(sAdd,[DataSet.Name,SearchListToStr(SearchListType),sKeyFieldName,sCalcFieldName]);
  lstCalculate.Add(sAdd);
end;

{------------------------------------------------------------------------------}
function TdmBasic_Custom.GetDescription(SearchListType:TSearchList;sPAKeyValue:string; bMultiLanguage:boolean=False):variant;
var
  sTableName_: string;
  sKeyName_:string;
  sReturnName_: string;
begin
  sTableName_ := ArrGetDescription[SearchListType, 1];
  sKeyName_ := ArrGetDescription[SearchListType, 2];
  sReturnName_ := ArrGetDescription[SearchListType, 3];
//  if (ArrGetDescription[SearchListType, 1] = EmptyStr) or
//     (ArrGetDescription[SearchListType, 2] = EmptyStr) or (sKeyFieldName_ = EmptyStr) then Exit;
  Result := UniMainModule.GetDescription(sTableName_,sKeyName_,sReturnName_, sPAKeyValue);
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.doCalculate(DataSet: TDataset);
begin
  doCalculate(TADODataset(DataSet), lstCalculate);
end;

{------------------------------------------------------------------------------}
function StrToSearchList(sSL: string): TSearchList;
begin
  Result := TSearchList(GetEnumValue(TypeInfo(TSearchList), sSL));
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.doCalculate(DataSet: TADODataset; lstPACalculate: TstringList);
var
  i: Integer;
  sOu1, sOu2, sOu3, sOu4, sOu5: string;
  SearchTypeListOption:TSearchList;
  procedure doSplite(s: string; var sOut1, sOut2, sOut3, sOut4, sOut5: string);
  var
    lst: TStrings;
  begin
    lst := TStringList.Create;
    try
      lst.Text := StringReplace(s, cSpliter, #13#10, [rfReplaceAll, rfIgnoreCase]);
      if lst.Count < 5 then exit;
      sOut1 := lst[0];
      sOut2 := lst[1];
      sOut3 := lst[2];
      sOut4 := lst[3];
      sOut5 := lst[4];
    finally
      lst.Free;
    end;
  end;
begin
  if DataSet = nil then exit;
  if DataSet.Active = False then Exit;
  for I := 0 to lstPACalculate.Count - 1 do
  begin
      // e.g. lstPACalculate[i]: {dsMaster|slItem|fldItemCode|fldItemName|bMultiLanguage}
         {  sOut1 = dsMaster sOut2 = slItem sOut3 = fldItemCode sOut4 = fldItemName  sOut = True}
      doSplite(lstPACalculate[i], sOu1, sOu2, sOu3, sOu4, sOu5);
//      if CompareText(sOu1, Dataset.Name) = 0 then
      if SameText(sOu1, Dataset.Name) then
      begin
         SearchTypeListOption := StrToSearchList(sOu2);
         if (DataSet.FindField(sOu3) <> nil) and (DataSet.FindField(sOu4) <> nil) then
         try
           DataSet.FieldByName(sOu4).Value := GetDescription(SearchTypeListOption,DataSet.FieldByName(sOu3).Asstring,StrToBool(sOu5));
           { TODO -oZheng Jie -cPG : Add a calculate Field value }
         except
         end;
      end;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.DataModuleCreate(Sender: TObject);
begin
  lstCalculate := TStringList.Create;
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.doSetFieldVisible(FieldVisible:boolean);
var
  i: Integer;
  C: TComponent;
begin
  if FieldVisible then
    Exit;
  for i := 0 to self.ComponentCount - 1 do
  begin
    C := self.Components[i];
    if C.Tag = 800 then
      if IsPublishedProp(C, 'OnGetText') then
      begin
        TField(C).OnGetText := dsfldGetText;
        TField(C).OnSetText := dsfldSetText;
//        TField(C).ReadOnly := True;
      end;
  end;
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.DataModuleDestroy(Sender: TObject);
begin
  lstCalculate.Free;
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.dsfldGetText(Sender: TField; var Text: String; DisplayText: Boolean);
begin

  // doNothing
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.dsfldSetText(Sender: TField; const Text: String);
begin

  // doNothing
end;

{------------------------------------------------------------------------------}
procedure TdmBasic_Custom.SetPriceVisible(const Value: Boolean);
begin
  FPriceVisible := Value;
  doSetFieldVisible(Value);
end;

end.
